/*
 * SoarAlert 1.6
 * Author:Dapeng
 * update 1.6 为success，error方法加入timeLimit参数，可定义自动关闭延迟时间
 * update 1.5 修正在非frame中调用错误的问题
 */
jQuery.dialogBox = {
    initSetting: function(options) {
        switch (options.type) {
            case 'autotip':
                this.setting.isShowIcon = false;
                this.setting.isShowConfirm = false;
                this.setting.isShowCancel = false;
                this.setting.isShowTitle = false;
                this.setting.isIframe = false;
                this.setting.isShowMask = false;
                this.setting.isShowClose = false;
                this.setting.disableClose = true;
                break;
            case 'tip':
                this.setting.title = '提示信息';
                this.setting.icon = 'icon_tip';
                break;
            case 'confirm':
                this.setting.title = '提示信息';
                this.setting.icon = 'icon_confirm';
                this.setting.isShowCancel = true;
                break;
            case 'error':
                this.setting.title = '警告';
                this.setting.icon = 'icon_error';
                break;
            case 'right':
                this.setting.title = '完成';
                this.setting.icon = 'icon_right';
                break;
            case 'element':
                this.setting.title = '提示信息';
                this.setting.isShowIcon = false;
                break;
            case 'page':
                this.setting.isShowIcon = false;
                this.setting.isShowConfirm = false;
                this.setting.isShowCancel = false;
                this.setting.isShowTitle = false;
                this.setting.isIframe = true;
                break;
            default:
                options.type = 'tip';
                this.setting.title = '提示信息';
                this.setting.icon = 'icon_tip';
				this.setting.isShowIcon = false;
                this.setting.isShowConfirm = false;
                this.setting.isShowCancel = false;
                this.setting.isShowTitle = false;
                break;
        }
        $.extend(this.setting, options);
    },
    resizeBind: false,
    scrollBind: false,
    skinLoaded: false,
    setting: {},
    defaults: {
        title: '',
        content: '',
        theme: 'simple',
        themePath: '',
        type: 'tip', //tip|error|right|confirm|element|page
        mask: {
            color: '#000',
            alpha: 0.5
        },
        boxSize: {
            width: '80%',
            height: ''
        },
        onlyDialogTop: true, //只在顶层框架打开
        btnClose: true,
        zIndex: 15000,
        confirmText: '确认',
        cancelText: '取消',
        confirmFn: null,
        cancelFn: null,
        openFn: null,
        closeFn: null,
        maskTime: 300,
        boxTime: 600,
        buttonAlign: 'right', //left|center|right
        icon: '',
        isShowIcon: true,
        isShowConfirm: true,
        isShowCancel: false,
        isShowTitle: false,
        isIframe: false,
        isShowClose: false,
        isShowMask: true,
        iframeTransparent: true,
        disableClose: false,
        autoClose: 0,
        autoTipType: 'success'			//error | watting | success   Default:success , Other:success
    },
    object: {
        window: $(window),
        container: $('body'),
        mask: $('<div></div>'),
        box: $('<div></div>'),
        titleBar: $('<div></div>'),
        buttonBar: $('<div></div>'),
        conBox: $('<div></div>'),
        titleText: $('<p></p>'),
        btnClose: $('<a></a>'),
        btnConfirm: $('<a></a>'),
        btnCancel: $('<a></a>'),
        conImgBox: $('<p></p>'),
        conTextBox: $('<p></p>')
    },
    open: function(options) {
        $.extend(this.setting, this.defaults);
        //弹出框容器
        if (this.setting.onlyDialogTop && window.top != window.self) {
            this.object.window = $(window.top);
            this.object.constructor = $(window.top.document).find('body');
        } else {
            this.object.constructor = $('body');
        }
        
        if (!$.isPlainObject(options)) {
            this.setting.content = options;
        }
        ;
        this.initSetting(options);
        this.constitute();
        if (this.setting.isShowTitle) {
            this.object.titleText.html(this.setting.title);
        }
        if (this.setting.type == 'page') {
            if (this.setting.isIframe) {
                var objIframe = $('<iframe frameborder="0" src="about:blank"></iframe>');
                objIframe.attr('allowTransparency', 'true');
                objIframe.attr('src', this.setting.content).css({
                    width: this.setting.boxSize.width,
                    height: this.setting.boxSize.height
                });
                objIframe.appendTo(this.object.conBox);
            } else {
                $.ajaxSetup({
                    error: function(jqXHR, textStatus, errorThrown) {
                        //alert('异步访问失败！\n' + textStatus + '\n' + XMLHttpRequest.status + '\n' + XMLHttpRequest.statusText + '\n' + errorThrown);
                    },
                    async: false
                });
                var html = '';
                $.get(this.setting.content, function(data) {
                    html = data;
                }, 'html');
                this.object.conBox.html(html);
            }
        } else if (this.setting.type == 'element') {
            this.object.conBox.html(this.setting.content);
        } else {
            this.object.conTextBox.html(this.setting.content);
        }
        if (this.setting.isShowMask) {
            this.showMask();
        }
        this.showBox();
        if (this.setting.autoClose > 0) {
            setTimeout(function() {
                jQuery.dialogBox.close();
            }, this.setting.autoClose);
        }
        return;
    },
    watting: function(message) {
        content = message == '' || message == undefined ? '在努力加载中，请稍候...' : message;
        $.dialogBox.open({
            'type': 'autotip',
            'content': content,
            'autoTipType': 'watting',
            'isShowMask': true
        })
    },
    success: function(message, closeLimit) {
        autoClose = parseInt(closeLimit) > 0 ? closeLimit : 3000;
        $.dialogBox.open({
            'type': 'autotip',
            'content': message,
            'autoTipType': 'success',
            'autoClose': autoClose
        })
    },
    error: function(message, closeLimit) {
        autoClose = parseInt(closeLimit) > 0 ? closeLimit : 3000;
        $.dialogBox.open({
            'type': 'autotip',
            'content': message,
            'autoTipType': 'error',
            'autoClose': autoClose
        })
    },
	tips:function(message){
		$.dialogBox.open({
            'type': 'tip',
            'content': message,
            isShowConfirm: true,
			confirmText:'知道了'
        })
	},
    showMask: function() {
        this.object.mask.css({
            background: this.setting.mask.color,
            'z-index': this.setting.zIndex,
            top: '0px',
            left: '0px',
            width: '100%',
            height: this.object.window.height(),
            position: 'fixed'
        });
        this.object.mask.animate({
            opacity: 0
        }, 0);

        this.object.mask.appendTo(this.object.constructor);
        this.object.mask.animate({
            opacity: this.setting.mask.alpha
        }, this.setting.maskTime);
    },
    showBox: function() {
        this.object.box.fadeIn(this.setting.boxTime, function() {
            if (jQuery.dialogBox.setting.disableClose == false) {
                jQuery.dialogBox.object.constructor.bind('click', jQuery.dialogBox.closeHandle);
            }
        });
        this.bindEvent();
        if (this.setting.openFn != null) {
            this.setting.openFn();
        }
    },
    //构建元素框架
    constitute: function() {
        //构建标题栏
        if (this.setting.isShowTitle) {
            this.object.titleText.addClass('title_text').css({
                'float': 'left'
            }).appendTo(this.object.titleBar);
            this.object.titleBar.addClass('title_box').appendTo(this.object.box);
        }
        //构建关闭按钮
        if (this.setting.isShowClose && !this.setting.disableClose) {
			this.object.btnClose.addClass('btn_close').text('X');
            if (this.setting.isShowTitle) {
                this.object.btnClose.appendTo(this.object.titleBar);
            } else {
                this.object.btnClose.css({
                    'z-index': this.setting.zIndex + 5
                }).appendTo(this.object.box);
            }
        }
        //构建内容框
        if (this.setting.isShowIcon) {
            this.object.conImgBox.removeClass().addClass(this.object.icon).appendTo(this.object.conBox);
        }
        if (this.setting.type != 'page' && this.setting.type != 'element' && this.setting.type != 'autotip') {
            this.object.conTextBox.addClass('con_text_box').appendTo(this.object.conBox);
            this.object.conBox.addClass('con_box').appendTo(this.object.box);
        } else if (this.setting.type == 'autotip') {
            switch (this.setting.autoTipType) {
                case 'error':
                    this.object.conTextBox.addClass('autotip_error_txt_box');
                    this.object.conBox.addClass('autotip_error_box')
                case 'watting':
                    this.object.conTextBox.addClass('autotip_watting_txt_box');
                    this.object.conBox.addClass('autotip_watting_box');
                default:
                    this.object.conTextBox.addClass('autotip_success_txt_box');
                    this.object.conBox.addClass('autotip_success_box');
                    break;
            }
            this.object.conTextBox.appendTo(this.object.conBox);
            this.object.conBox.appendTo(this.object.box);
            this.object.box.css({'background-color': 'transparent', 'box-shadow': 'none'});
        } else {
            this.object.conTextBox.appendTo(this.object.conBox);
            this.object.conBox.appendTo(this.object.box);
            this.object.box.css({'background-color': 'transparent', 'box-shadow': 'none'});
        }


        //构建按纽栏
        if ((this.setting.isShowConfirm || this.setting.isShowCancel)) {
            var buttonWitdh = '100%';
			if(this.setting.isShowConfirm && this.setting.isShowCancel){
				buttonWitdh = '50%';
				this.object.btnConfirm.css({"border-bottom-right-radius":"0"});
				this.object.btnCancel.css({"border-bottom-left-radius":"0"});
			}
			if (this.setting.isShowConfirm) {
                this.object.btnConfirm.addClass('btn_confirm')
						.css({'width':buttonWitdh})
                        .html(this.setting.confirmText)
                        .appendTo(this.object.buttonBar);
            } else {
                this.object.btnCancel.remove();
            }
            if (this.setting.isShowCancel) {
                this.object.btnCancel.addClass('btn_cancel')
						.css({'width':buttonWitdh})
                        .html(this.setting.cancelText)
                        .appendTo(this.object.buttonBar);
            } else {
                this.object.btnCancel.remove();
            }
            this.object.buttonBar.addClass('button_bar')
                    .css({
                'text-align': this.setting.buttonAlign
            })
                    .appendTo(this.object.box);
        }


        //组织对话框
        this.object.box.addClass('jqueryDialogBox').css({
            'z-index': this.setting.zIndex + 1,
            width: this.setting.boxSize.width,
            height: this.setting.boxSize.height,
            display: 'none',
            position: 'absolute'
        });
        if (this.setting.type == 'page') {
            this.object.box.css({
                border: '0px',
                background: 'transparent'
            });
        }
        this.object.box.appendTo(this.object.constructor);
    },
    //事件绑定
    bindEvent: function() {
        this.object.btnConfirm.bind('click', function() {
            if (jQuery.dialogBox.setting.confirmFn == null) {
                jQuery.dialogBox.close();
            } else {
                jQuery.dialogBox.setting.confirmFn();
            }
        })
        this.object.btnCancel.bind('click', function() {
            if (jQuery.dialogBox.setting.cancelFn != null) {
                jQuery.dialogBox.setting.cancelFn();
            }
            jQuery.dialogBox.close();
        })
        this.object.btnClose.bind('click', function() {
            jQuery.dialogBox.close();
        });
        this.adjustPosition();
        if (!this.resizeBind) {
            this.object.window.resize(function() {
                jQuery.dialogBox.adjustPosition();
            });
            this.resizeBind = true;
        }
        this.object.box.css('position', 'fixed');
    },
    close: function(fn) {
        if ($.isFunction(fn)) {
            jQuery.dialogBox.setting.closeFn = fn;
        }
        jQuery.dialogBox.object.btnClose.unbind('click');
        jQuery.dialogBox.object.btnCancel.unbind('click');
        jQuery.dialogBox.object.btnConfirm.unbind('click');
        jQuery.dialogBox.object.box.fadeOut(250, function() {
            jQuery.dialogBox.object.mask.animate({
                opacity: 0
            }, 250, function() {
                $.each(jQuery.dialogBox.object, function(index, val) {
                    val.removeClass();
                });
                jQuery.dialogBox.object.mask.remove();
                jQuery.dialogBox.object.conBox.html('');
                jQuery.dialogBox.object.box.remove();
                jQuery.dialogBox.object.box.html('');
                jQuery.dialogBox.object.window = $(window);
                jQuery.dialogBox.object.container = $('body');
                jQuery.dialogBox.object.mask = $('<div></div>');
                jQuery.dialogBox.object.box = $('<div></div>');
                jQuery.dialogBox.object.titleBar = $('<div></div>');
                jQuery.dialogBox.object.buttonBar = $('<div></div>');
                jQuery.dialogBox.object.conBox = $('<div></div>');
                jQuery.dialogBox.object.titleText = $('<p></p>');
                jQuery.dialogBox.object.btnClose = $('<a></a>');
                jQuery.dialogBox.object.btnConfirm = $('<a></a>');
                jQuery.dialogBox.object.btnCancel = $('<a></a>');
                jQuery.dialogBox.object.conImgBox = $('<p></p>');
                jQuery.dialogBox.object.conTextBox = $('<p></p>');
                if (jQuery.dialogBox.setting.closeFn != null) {
                    jQuery.dialogBox.setting.closeFn();
                }
            });
        });
        if (jQuery.dialogBox.setting.disableClose == false) {
            jQuery.dialogBox.object.constructor.unbind('click', jQuery.dialogBox.closeHandle);
        }
    },
    adjustPosition: function() {
        var intScrollTop = Math.round((this.object.window.height() - this.object.box.height()) / 2 * 0.75);
        var intScrollLeft = Math.round((this.object.window.width() - this.object.box.width()) / 2) + this.object.window.scrollLeft();
        this.object.box.css({
            top: intScrollTop,
            left: intScrollLeft
        });
        this.object.mask.height(this.object.window.height());
    },
    closeHandle: function(e) {
        if ($(e.target).parents('.jqueryDialogBox').length == 0) {
            jQuery.dialogBox.close();
        }
    }
};
